﻿
function Dsy(){
	this.Items = {};
}
Dsy.prototype.add = function(id,iArray){
	this.Items[id] = iArray;
}
Dsy.prototype.Exists = function(id){
	if(typeof(this.Items[id]) == "undefined") return false;
	return true;
}

function change(v,al){
	
	var str="0";
	var selectNum;
	for(i=0;i<v;i++){
         if(i==0){
		 selectNum=Number(dsy.Items["0"].indexOf($("#"+s[i]+" option:selected").val()))
			}
		   else{
		selectNum=document.getElementById(s[i]).selectedIndex-1   
			   }
		str+=("_"+selectNum);
		
	};
	var ss=document.getElementById(s[v]);
	with(ss){
		length = 0;
		options[0]=new Option(opt0[v],opt0[v]);
		if(v && document.getElementById(s[v-1]).selectedIndex>0 || !v){
			if(dsy.Exists(str)){
				localStorage.cityCode=str;
				ar = dsy.Items[str];
				for(i=0;i<ar.length;i++){			
					if(al==1&&i>0&&v<=1){break}
					if(al==2&&v==0&&i!=0&&i!=2&&i!=10){
						continue;
						}
					if(al==2&&v==1&&i>0){
						continue;
						}
					if(al==3&&i==0&&v==0){continue}
					options[length]=new Option(ar[i],ar[i]);
					
				}//end for

				if(v){ options[0].selected = true; }
			}
		}//end if v	
		if(++v<s.length){change(v);}
	}//End with
	
}

var dsy = new Dsy();
dsy.add("0",["北京市","天津市","上海市","河北省","江苏省","浙江省","山东省","广东省"]);
dsy.add("0_0_0",["东城区","西城区","崇文区","宣武区","朝阳区","丰台区","石景山区","海淀区","门头沟区","房山区","通州区","顺义区","昌平区","大兴区","怀柔区","平谷区","密云县","延庆县","延庆镇"]);
dsy.add("0_0",["北京市"]);
dsy.add("0_1_0",["和平区","河东区","河西区","南开区","河北区","红桥区","塘沽区","汉沽区","大港区","东丽区","西青区","津南区","北辰区","武清区","宝坻区","蓟县","宁河县","芦台镇","静海县","静海镇"]);
dsy.add("0_1",["天津市"]);
dsy.add("0_2_0",["黄浦区","卢湾区","徐汇区","长宁区","静安区","普陀区","闸北区","虹口区","杨浦区","闵行区","宝山区","嘉定区","浦东新区","金山区","松江区","青浦区","南汇区","奉贤区","崇明县","城桥镇"]);
dsy.add("0_2",["上海市"]);

dsy.add("0_3_0",["长安区","桥东区","桥西区","新华区","裕华区","井陉矿区","辛集市","藁城市","晋州市","新乐市","鹿泉市","井陉县","微水镇","正定县","正定镇","栾城县","栾城镇","行唐县","龙州镇","灵寿县","灵寿镇","高邑县","高邑镇","深泽县","深泽镇","赞皇县","赞皇镇","无极县","无极镇","平山县","平山镇","元氏县","槐阳镇","赵县","赵州镇"]);
dsy.add("0_3_1",["路北区","路南区","古冶区","开平区","丰润区","丰南区","遵化市","迁安市","滦县","滦州镇","滦南县","倴城镇","乐亭县","乐亭镇","迁西县","兴城镇","玉田县","玉田镇","唐海县","唐海镇"]);
dsy.add("0_3_2",["安次区","广阳区","霸州市","三河市","固安县","固安镇","永清县","永清镇","香河县","淑阳镇","大城县","平舒镇","文安县","文安镇","大厂回族自治县","大厂镇"]);

dsy.add("0_3",["石家庄市","唐山市","廊坊市"]);

dsy.add("0_4_0",["崇安区","南长区","北塘区","滨湖区","惠山区","锡山区","江阴市","宜兴市"]);
dsy.add("0_4_1",["金阊区","沧浪区","平江区","虎丘区","吴中区","相城区","吴江市","昆山市","太仓市","常熟市","张家港市"]);
dsy.add("0_4",["无锡市","苏州市"]);
dsy.add("0_5_0",["拱墅区","上城区","下城区","江干区","西湖区","滨江区","余杭区","萧山区","临安市","富阳市","建德市","桐庐县","淳安县","千岛湖镇"]);

dsy.add("0_5",["杭州市"]);


dsy.add("0_6_0",["市中区","历下区","槐荫区","天桥区","历城区","长清区","章丘市","平阴县","平阴镇","济阳县","济阳镇","商河县"]);
dsy.add("0_6_1",["市南区","市北区","四方区","黄岛区","崂山区","城阳区","李沧区","胶州市","即墨市","平度市","胶南市","莱西市"]);
dsy.add("0_6_2",["东昌府区","临清市","阳谷县","莘县","茌平县","东阿县","冠县","冠城镇","高唐县"]);
dsy.add("0_6_3",["德城区","乐陵市","禹城市","陵县","陵城镇","平原县","夏津县","夏津镇","武城县","武城镇","齐河县","晏城镇","临邑县","宁津县","宁津镇","庆云县","庆云镇"]);
dsy.add("0_6_4",["东营区","河口区","垦利县","垦利镇","利津县","利津镇","广饶县","广饶镇"]);
dsy.add("0_6_5",["张店区","淄川区","博山区","临淄区","周村区","桓台县","索镇","高青县","田镇","沂源县","南麻镇"]);
dsy.add("0_6_6",["潍城区","寒亭区","坊子区","奎文区","安丘市","昌邑市","高密市","青州市","诸城市","寿光市","临朐县","昌乐县"]);
dsy.add("0_6_7",["莱山区","芝罘区","福山区","牟平区","栖霞市","海阳市","龙口市","莱阳市","莱州市","蓬莱市","招远市","长岛县","南长山镇"]);
dsy.add("0_6_8",["环翠区","荣成市","乳山市","文登市"]);
dsy.add("0_6_9",["东港区","岚山区","五莲县","洪凝镇","莒县","城阳镇"]);
dsy.add("0_6_10",["兰山区","罗庄区","河东区","郯城县","郯城镇","苍山县","卞庄镇","莒南县","十字路镇","沂水县","沂水镇","蒙阴县","蒙阴镇","平邑县","平邑镇","费县","费城镇","沂南县","界湖镇","临沭县","临沭镇"]);
dsy.add("0_6_11",["薛城区","市中区","峄城区","台儿庄区","山亭区","滕州市"]);
dsy.add("0_6_12",["市中区","任城区","曲阜市","兖州市","邹城市","微山县","鱼台县","谷亭镇","金乡县","金乡镇","嘉祥县","嘉祥镇","汶上县","汶上镇","泗水县","梁山县","梁山镇"]);
dsy.add("0_6_13",["泰山区","岱岳区","新泰市","肥城市","宁阳县","宁阳镇","东平县","东平镇"]);
dsy.add("0_6_14",["莱城区","钢城区"]);
dsy.add("0_6_15",["滨城区","惠民县","惠民镇","阳信县","阳信镇","无棣县","无棣镇","沾化县","富国镇","博兴县","博兴镇","邹平县"]);
dsy.add("0_6_16",["牡丹区","曹县","曹城镇","定陶县","定陶镇","成武县","成武镇","单县","单城镇","巨野县","巨野镇","郓城县","郓城镇","鄄城县","鄄城镇","东明县","城关镇"]);
dsy.add("0_6",["济南市","青岛市","聊城市","德州市","东营市","淄博市","潍坊市","烟台市","威海市","日照市","临沂市","枣庄市","济宁市","泰安市","莱芜市","滨州市","菏泽市"]);



dsy.add("0_7_0",["越秀区","荔湾区","海珠区","天河区","白云区","黄埔区","番禺区","花都区","南沙区","萝岗区","增城市","从化市"]);
dsy.add("0_7_1",["福田区","罗湖区","南山区","宝安区","龙岗区","盐田区"]);

dsy.add("0_7_2",["莞城街道","南城街道","东城街道","万江街道","石龙镇","石排镇","茶山镇","企石镇","桥头镇","东坑镇","横沥镇","常平镇","虎门镇","长安镇","沙田镇","厚街镇","寮步镇","大岭山镇","大朗镇","黄江镇","樟木头镇","谢岗镇","塘厦镇","清溪镇","凤岗镇","麻涌镇","中堂镇","高埗镇","石碣镇","望牛墩镇","洪梅镇","道滘镇"]);

dsy.add("0_7_3",["禅城区","南海区","顺德区","三水区","高明区"]);

dsy.add("0_7",["广州市","深圳市","东莞市","佛山市"]);


dsy.add("0",["北京市","天津市","上海市","河北省","江苏省","浙江省","山东省","广东省"]);

var s=["s_province","s_city","s_county"];//三个select的name
var opt0 = ["省份","地级市","市、县级市"];//初始值
function _init_area(al,pr,ci,co,ad,name,tel){  //初始化函数,al为areaLimit,是否地区限制，值为true和fasle;
	for(i=0;i<s.length-1;i++){
	  document.getElementById(s[i]).onchange=new Function("change("+(i+1)+","+al+")");
	}
	change(0,al);
	function setAddress(){
	     $("#contactName").val(name);
		 $("#contactTel").val(tel);
		 $("#applyName").html(name);
		 $("#applyTel").html(tel);
		var m_province=Number(dsy.Items["0"].indexOf(pr));
		if(m_province==-1){
			change(0,al)
			return;
			}
		$("#"+s[0]+" option").eq(m_province+1).attr("selected","selected");
		selectSpan();
		change(1,al);
		var m_city=Number(dsy.Items["0_"+m_province].indexOf(ci));
		if(m_city==-1){
			change(0,al)
			return;
			}
		$("#"+s[1]+" option").eq(m_city+1).attr("selected","selected");
		selectSpan();
		change(2,al);
		var m_county=Number(dsy.Items["0_"+m_province+"_"+m_city].indexOf(co));
		if(m_county==-1){
			change(0,al)
			return;
			}
		$("#"+s[2]+" option").eq(m_county+1).attr("selected","selected");
		selectSpan();
		if($("#"+s[2]+" option:selected").val()!="市、县级市"){
			$("#contactAddress").val(ad);
			if(pr==ci){
			ci="";
			 }
			$("#applyAddress").html(pr+ci+co+ad);
			}
		}
		setAddress();
		setTimeout(function(){
			if($("#applyName").html()!=""&&$("#applyTel").html()!=""&&$("#applyAddress").html()!=""&&$("#contactAddress").val()!=""){
				$(".addressLiDetail").show();
				}
			},300)
}
$("#"+s[0]).change(function(){
	  
		 var selectedPro=$("#"+s[0]+" option:selected").val();
		if(selectedPro=="北京市"||selectedPro=="上海市"||selectedPro=="天津市"||selectedPro=="重庆市"){
			change(2,0);
			
		setTimeout(function(){
			$("#s_city option").eq(1).attr("selected","selected");	
			$(".applySelectTitle").html("收货地址"+$("#"+s[1]+" option").eq(1).html())
			selectSpan();
			change(2,0);
			},200)
			
			
			}
	 
	})